package cn.wk.libs4a.view.gallery;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.wk.libs4a.R;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.WKBaseActivity;

/**
 * 滑动相册
 * 
 * Intent intent = new Intent(context, GalleryActivity.class); String[] images =
 * new String[imgUrlList.size()]; imgUrlList.toArray(images);
 * intent.putExtra("images", images); context.startActivity(intent);
 */
public class WKGalleryActivity extends WKBaseActivity {
	private TextView viewpager_left_num;
	private TextView viewpager_right_num;

	private ViewGroup produceRoot;
	private ViewPager viewPager;

	private String[] originList;
	private int clickPosition;
	private ImageView comment_gridview_download_bg;
	private String imagePath;
	SamplePagerAdapter adapter;
	private boolean isSelected = false;
	private ProgressDialog pd;
	private String msg = "";

	@Override
	public void loadXml() {
		produceRoot = (ViewGroup) View.inflate(ctx, R.layout.wk_gallery, null);
		setContentView(produceRoot);
	}

	@Override
	public void init() {
		
		Intent intent = getIntent();
		clickPosition = intent.getIntExtra("clickPosition", 0);
		originList = intent.getStringArrayExtra("images");
		
		findView();
		setListener();
	}

	private void findView() {
		viewpager_left_num = (TextView) findViewById(R.id.viewpager_left_num);
		viewpager_right_num = (TextView) findViewById(R.id.viewpager_right_num);
		viewPager = (WKViewPager) findViewById(R.id.producer_viewPager_view);
		comment_gridview_download_bg = (ImageView) findViewById(R.id.comment_gridview_download_bg);
		adapter = new SamplePagerAdapter();
	}

	private void setListener() {
		comment_gridview_download_bg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new AsyncTask<Void, Void, String>() {
					@Override
					protected void onPreExecute() {
						super.onPreExecute();
						pd = new ProgressDialog(ctx);
						pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
						pd.setMessage("正在下载中...");
						pd.show();
					}

					@Override
					protected String doInBackground(Void... params) {
						// try {
						// if (TextUtils.isEmpty(imagePath))
						// imagePath = originList[0];
						// app().imageLoader.downLoadImage(imagePath);
						// String str = app().imageLoader
						// .getFilePath(imagePath);
						// File f1 = new File(str);
						// String fn = FileTools.getFileName(imagePath);
						// File f2 = new File(WKApplication.DCIM, Md5Util
						// .getMD5Str(fn) + ".jpg");
						// FileTools.copyFile(f1, f2);
						// msg = "下载到相册成功!";
						// } catch (Exception e) {
						// e.printStackTrace();
						// msg = "下载失败!";
						// }
						return msg;
					}

					@Override
					protected void onPostExecute(String result) {
						try {
							pd.dismiss();
							WKApplication.showMsg(msg);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}.execute();
			}
		});
	}

	@Override
	public void setData() {

	}

	class SamplePagerAdapter extends PagerAdapter {
		ViewGroup viewGroup;

		@Override
		public int getCount() {
			if (originList == null)
				return 0;
			return originList.length;
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {

			View contentView = View.inflate(ctx, R.layout.listitem_gallery,
					null);
			WKPhotoViewForGallery photoView = (WKPhotoViewForGallery) contentView
					.findViewById(R.id.pager);
			ProgressBar pb = (ProgressBar) contentView
					.findViewById(R.id.progress);
			photoView.setProgressBar(pb);
			photoView.setBigUrl(originList[position].replace(" ", "%20"));
			photoView.reload();
			((ViewPager) container).addView(contentView);
			viewGroup = container;
			return contentView;

		}

		public void cleanBitmap() {
			try {
				if (viewGroup != null) {
					for (int i = 0; i < viewGroup.getChildCount(); i++) {
						if (viewGroup.getChildAt(i).findViewById(R.id.pager) != null) {
							((WKPhotoViewForGallery) viewGroup.getChildAt(i)
									.findViewById(R.id.pager)).recycle(true);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// WKPhotoViewForGallery page = (WKPhotoViewForGallery) ((View)
			// object)
			// .findViewById(R.id.pager);
			// if (page != null) {
			// page.recycle();
			// }
			// container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}

	class CommentPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int position) {
			viewpager_left_num.setText((position + 1) + "");

			imagePath = originList[position];
			System.out.println("imagePath----------------" + imagePath);
			if (position == 0 && isSelected) {
				WKApplication.showToast("已经到第一页!");
			}
			if (position == originList.length - 1 && isSelected) {
				WKApplication.showToast("已经到最后一页!");
			}
			isSelected = true;
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		// if (adapter != null)
		// adapter.cleanBitmap();
	}

	@Override
	protected void onResume() {
		super.onResume();
		try {
			viewPager.setAdapter(adapter);
			viewPager.setOnPageChangeListener(new CommentPageChangeListener());
			viewPager.setCurrentItem(clickPosition);
			viewpager_left_num.setText(clickPosition + 1 + "");
			viewpager_right_num.setText(originList.length + "");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
